﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Components
{
    public class DataTableHelper
    {
        private static DataTableHelper _entity;

        public static DataTableHelper Entity
        {
            get
            {
                if (null == _entity)
                    _entity = new DataTableHelper();
                return DataTableHelper._entity;
            }
        }


        /// <summary>
        /// 为DataTable添加列
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="cols"></param>
        public DataTable MarkColumn(DataTable dt, params string[] cols)
        {
            if (dt == null)
            {
                dt = new DataTable();
                dt.TableName = "DTLDATA";
            }

            if (null == cols || 0 == cols.Length) return null;

            DataColumnCollection dcc = dt.Columns;
            foreach (string col in cols)
            {
                if (dcc.Contains(col)) continue; //已经存在该列

                dt.Columns.Add(new DataColumn(col));
            }

            return dt;
        }

        /// <summary>
        /// 验证DataTable是否为空
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool IsEmptyTable(DataTable data)
        {
            return null == data || 0 == data.Rows.Count;
        }
    }
}
